<?php

namespace App\Admin\Controllers;

use App\Models\Food;
use App\Http\Controllers\Controller;
use App\Models\Scene;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class FoodController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('美食管理')
            ->description('美食')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Food);

        $grid->id('ID')->sortable();
        $grid->map()->display(function($map){
            return $map==1?'概览':'详细';
        });
        $grid->loc();
        $grid->type()->display(function($type){
            return $type==1?'美食推荐':'美食街';
        });
        $grid->menu_id();
        $grid->title();
        $grid->coverimage()->image('http://media.weijingguanjia.com/',36,36);
        $grid->bannerimage()->display(function ($images) {
            return $images;
        })->image('http://media.weijingguanjia.com/', 36, 36);

        $grid->recommended()->using([1 => '很差', '2' => '一般',3=>'良好',4=>'满意',5=>'非常满意'])->badge();
        $grid->columns('price','tag','address','lanter','iphone','scene_id');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Food::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Food);
        $form->radio('map', '地图')->options(['1' => '概览', '2'=> '详细']);
        $form->text('loc', '手绘定位');
        $form->radio('type', '類型')->options(['1' => '美食推荐', '2'=> '美食街']);
        $form->select('menu_id','所属菜单')->options('/api/food/menu');
        $form->text('title', '标题')->rules('required');
        $form->textarea('content', '内容')->rules('required');
        $form->select('recommended','推荐指数')->options(['1' => '很差', '2'=> '一般', '3'=> '良好', '4'=> '满意', '5'=> '非常满意']);
        $form->number('price','人均消费');
        $form->image('coverimage','封面图片')->uniqueName()->removable()->rules('required');
        $form->multipleImage('bannerimage','轮播图片')->uniqueName()->removable();
        $form->tags('tag', '标语')->rules('required');
        $form->text('address', '地址');
        $form->text('lanter', '经纬度')->rules('required');
        $form->text('iphone', '电话');
        $form->select('scene_id','所属景点')->options(Scene::all()->pluck('title','id'));
        return $form;
    }
}
